package com.cws.project.cws.course.controller;

import java.util.List;
import java.util.stream.Collectors;

import com.cws.common.utils.StringUtils;
import com.cws.common.utils.poi.ExcelUtil;
import com.cws.framework.aspectj.lang.annotation.Log;
import com.cws.framework.aspectj.lang.enums.BusinessType;
import com.cws.framework.web.controller.BaseController;
import com.cws.framework.web.domain.AjaxResult;
import com.cws.framework.web.page.TableDataInfo;
import com.cws.project.cws.clazz.domain.Clazz;
import com.cws.project.cws.clazz.domain.ClazzUser;
import com.cws.project.cws.clazz.service.IClazzService;
import com.cws.project.cws.clazz.service.IClazzUserService;
import com.cws.project.cws.teacher.domain.Teacher;
import com.cws.project.cws.teacher.service.ITeacherService;
import com.cws.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cws.project.cws.course.domain.Course;
import com.cws.project.cws.course.service.ICourseService;

import javax.servlet.http.HttpServletRequest;

/**
 * 课程信息Controller
 *
 * @author xyl
 * @date 2022-01-04
 */
@Controller
@RequestMapping("/cws/course")
public class CourseController extends BaseController {
    private String prefix = "cws/course";

    @Autowired
    private ICourseService courseService;
    @Autowired
    private ITeacherService teacherService;
    @Autowired
    private IClazzService clazzService;
    @Autowired
    private IClazzUserService clazzUserService;

    @RequiresPermissions("cws:course:view")
    @GetMapping()
    public String course() {
        return prefix + "/course";
    }

    @RequiresPermissions("cws:course:student:view")
    @GetMapping("/student")
    public String studentCourse(HttpServletRequest request,ModelMap mmap) {
        String jige = request.getParameter("jige");
        String studentId = request.getParameter("studentId");
        if(StringUtils.isNotNull(jige) && StringUtils.isNotNull(studentId)){
            mmap.put("jige",jige);
            mmap.put("studentId",studentId);
        }
        return prefix + "/student";
    }

    /**
     * 查询课程信息列表
     */
    @RequiresPermissions("cws:course:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Course course) {
        User sysUser = getSysUser();
        //辅导员 查看自己班的
        if(sysUser.isInstructor()){
            ClazzUser clazzUser = new ClazzUser();
            clazzUser.setUserId(sysUser.getUserId());
            course.setClazzIds(clazzUserService.selectClazzUserList(clazzUser).stream().map(ClazzUser::getClazzId).collect(Collectors.toList()));
        }
        //教师
        if(sysUser.isTeacher() || sysUser.isStudent()){
            course.setTeacherId(sysUser.getUserId());
        }
        startPage();
        List<Course> list = courseService.selectCourseList(course);
        return getDataTable(list);
    }
    /**
     * 查询课程信息列表
     */
    @PostMapping("/student/list")
    @ResponseBody
    public TableDataInfo studentList(Course course) {
        User sysUser = getSysUser();
        if(sysUser.isStudent()){
            course.setStudentId(sysUser.getUserId());
        }
        startPage();
        List<Course> list = courseService.selectStudentCourseList(course);
            return getDataTable(list);
    }

    /**
     * 导出课程信息列表
     */
    @RequiresPermissions("cws:course:export")
    @Log(title = "课程信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Course course) {
        User sysUser = getSysUser();
        //辅导员 查看自己班的
        if(sysUser.isInstructor()){
            ClazzUser clazzUser = new ClazzUser();
            clazzUser.setUserId(sysUser.getUserId());
            course.setClazzIds(clazzUserService.selectClazzUserList(clazzUser).stream().map(ClazzUser::getClazzId).collect(Collectors.toList()));
        }
        //教师
        if(sysUser.isTeacher() || sysUser.isStudent()){
            course.setTeacherId(sysUser.getUserId());
        }
        List<Course> list = courseService.selectCourseList(course);
        ExcelUtil<Course> util = new ExcelUtil<Course>(Course.class);
        return util.exportExcel(list, "course");
    }

    /**
     * 新增课程信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        //当前辅导员id，
        User sysUser = getSysUser();
        List<Teacher> teacherList;
        List<Clazz> clazzList;
        if(sysUser.isAdmin()){
            teacherList = teacherService.selectTeacherList(null);
            clazzList = clazzService.selectClazzList(null);
        }else{
            Teacher teacher = new Teacher();
            teacher.setUserId(sysUser.getUserId());
            teacherList = teacherService.selectTeacherList(teacher);
            clazzList = clazzService.selectClazzListByUserId(sysUser.getUserId());
        }
        mmap.put("teachers",teacherList);
        mmap.put("clazzs",clazzList);
        return prefix + "/add";
    }

    /**
     * 新增保存课程信息
     */
    @RequiresPermissions("cws:course:add")
    @Log(title = "课程信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Course course) {
        course.setCreateBy(getLoginName());
        return toAjax(courseService.insertCourse(course));
    }

    /**
     * 修改课程信息
     */
    @GetMapping("/edit/{courseId}")
    public String edit(@PathVariable("courseId") Long courseId, ModelMap mmap) {
        User sysUser = getSysUser();
        List<Teacher> teacherList;
        List<Clazz> clazzList;
        if(sysUser.isAdmin()){
            teacherList = teacherService.selectTeacherList(null);
            clazzList = clazzService.selectClazzList(null);
        }else{
            Teacher teacher = new Teacher();
            teacher.setUserId(sysUser.getUserId());
            teacherList = teacherService.selectTeacherList(teacher);

            clazzList = clazzService.selectClazzListByUserId(sysUser.getUserId());
        }
        mmap.put("teachers",teacherList);
        mmap.put("clazzs",clazzList);
        Course course = courseService.selectCourseById(courseId);
        mmap.put("course", course);
        return prefix + "/edit";
    }

    /**
     * 修改保存课程信息
     */
    @RequiresPermissions("cws:course:edit")
    @Log(title = "课程信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Course course) {
        return toAjax(courseService.updateCourse(course));
    }

    /**
     * 删除课程信息
     */
    @RequiresPermissions("cws:course:remove")
    @Log(title = "课程信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(courseService.deleteCourseByIds(ids));
    }
}
